package com.apes.scm.rbac.model;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：本地组织权限
 *
 * @author xul
 * @create 2017-12-15 13:44
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = LocalPermit.CONST_TABLE_NAME)
public class LocalPermit extends Model {
    public static final String CONST_TABLE_NAME = "aut_local_permit";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "本地权限")
    private String id;

    @Column(name = "name", length = 32, nullable = false)
    private String name;  //描述

    @OneToMany(mappedBy = "localPermit", cascade = CascadeType.ALL, orphanRemoval = true)
    @JSONField(serialize = false)
    private Set<LocalPermitItem> localPermitItems = new HashSet();

    public Set<LocalPermitItem> getLocalPermitItems() {
        if (localPermitItems == null) return null;
        for (LocalPermitItem item : localPermitItems) {
            item.setLocalPermit(this);
        }
        return localPermitItems;
    }

//    @ManyToMany
//    @JoinTable(name = "aut_post_permit",
//            joinColumns = {@JoinColumn(name = "local_permit_id", referencedColumnName = "id")},
//            inverseJoinColumns = {@JoinColumn(name = "post_id", referencedColumnName = "id")})
//    private Set<Post> assignedLocalPermits;



    /*
    * 操作界面转换Map
    * */
    @Transient
    private JSONObject permitPurposeBelong;  // 所属

    @Transient
    private JSONObject permitPurposeOperate;  // 操作

    @Transient
    private JSONObject permitPurposeSearch;  // 查询

}
